package com.mz.ozy.activity.me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mz.ozy.R;
import com.mz.ozy.adapter.MybounsDetailAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.FriendBean;
import com.mz.ozy.bean.MyBounsBean;
import com.mz.ozy.bean.MyBounsRecodeBean;
import com.mz.ozy.bean.NewBonusBean;
import com.mz.ozy.bean.NewBonusDetailBean;
import com.mz.ozy.bean.NewBonusDonateBean;
import com.mz.ozy.bean.NewGQBean;
import com.mz.ozy.bean.NewResponeBean;
import com.mz.ozy.bean.PronotionBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.MyBounsPresenter;
import com.mz.ozy.mvp.view.MyBonusContract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lifeStyle on 2020/4/9 0009.
 */
public class MyBonusDetailActivity extends BaseActivity<MyBonusContract.View, MyBounsPresenter> implements MyBonusContract.View {

    Integer tvTypeList[] = {1, 2, 3, 4, 5};
    Integer tvList[] = { R.id.tv_out, R.id.tv_withdraw, R.id.tv_exchange, R.id.tv_recode, R.id.tv_in};
    Integer tvViewList[] = {R.id.view_out, R.id.view_withdraw, R.id.view_exchange, R.id.view_recode, R.id.view_in};

    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.image_data)
    ImageView image_data;

    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private int pageNo = 1;
    private int tag = 0;
    private int type = 4;
    private List<NewBonusDetailBean.PageDTO.PageItemsDTO> mBeanList = new ArrayList<>();
    private MybounsDetailAdapter mAdapter;

    private String remain;
    private int bonusType;
    private NewBonusBean newBonusBean;

    public static void go(Context context, int type) {
        Intent intent = new Intent(context, MyBonusDetailActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    public static void go(Context context, int type, String remain) {
        Intent intent = new Intent(context, MyBonusDetailActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("remain", remain);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mybouns_detail);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText("奖励明细");
        image_data.setImageResource(R.mipmap.no_goods_icon);
        bonusType = getIntent().getIntExtra("type", 1);
        remain = getIntent().getStringExtra("remain");

        initView();

        mPresenter.newBonus(this);
    }

    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MybounsDetailAdapter(mBeanList, type, bonusType);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Intent intent = new Intent(MyBonusDetailActivity.this, MyBonusDetailInfoActivity.class);
//                MyBonusDetailActivity.this.startActivity(intent);
            }
        });

        recyclerview.setAdapter(mAdapter);
        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.newBonusDetail(MyBonusDetailActivity.this, pageNo + "", type +"");
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshList();
            }
        });
    }

    private void refreshList() {
        if (mPresenter == null) return;
        tag = 0;
        pageNo = 1;
        mPresenter.newBonusDetail(MyBonusDetailActivity.this, pageNo + "", type +"");
    }

    @OnClick({R.id.tv_recode, R.id.tv_out, R.id.tv_withdraw, R.id.tv_exchange, R.id.tv_in, R.id.tv_with, R.id.bnt_donate})
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.tv_with:
            {
//                startActivity(new Intent(MyBonusDetailActivity.this, MeWithActivity.class));
                MeNewWithActivity.go(this, newBonusBean.remain);
            }
                break;

            case R.id.bnt_donate:
            {
                startActivity(new Intent(MyBonusDetailActivity.this, MyBonusDonateActivity.class));
            }
                break;

            default:
            {
                for(int i = 0; i < tvList.length; i++) {
                    Integer tvtag = tvList[i];
                    Integer tvViewtag = tvViewList[i];
                    if (view.getId() == tvtag) {
                        type = tvTypeList[i];
                        mAdapter.setType(type);
                        ((TextView)findViewById(tvtag)).setTextColor(getResources().getColor(R.color.c_73b21a));
                        findViewById(tvViewtag).setVisibility(View.VISIBLE);
                    } else {
                        ((TextView)findViewById(tvtag)).setTextColor(getResources().getColor(R.color.c_a5a5a5));
                        findViewById(tvViewtag).setVisibility(View.GONE);
                    }
                }
                refreshList();
            }
                break;
        }
    }

    @Override
    public void getInfoSuccess(MyBounsBean bean) {

    }

    @Override
    public void getInfoRecode(MyBounsRecodeBean bean) {
//        if (tag == 0) {
//            smartRefreshLayout.setLoadmoreFinished(false);
//            smartRefreshLayout.finishRefresh();
//            mBeanList.clear();
//            if (bean.getPage().getPageItems().size() == 0) {
//                llNoData.setVisibility(View.VISIBLE);
//            } else {
//                llNoData.setVisibility(View.GONE);
//            }
//        } else if (tag == 1) {
//            smartRefreshLayout.finishLoadmore();
//        }
//        mBeanList.addAll(bean.getPage().getPageItems());
//        mAdapter.notifyDataSetChanged();
//        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
//            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
//        }
    }

    @Override
    public void getVipUpShSuccess(FriendBean bean) {

    }

    @Override
    public void getVipUpDataInfoSuccess(PronotionBean bean) {

    }

    @Override
    public void newBonusSuccess(NewBonusBean bean) {
        newBonusBean = bean;
        tvBalance.setText(newBonusBean.remain);
    }

    @Override
    public void newBonusDetailSuccess(NewBonusDetailBean bean) {
        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.page == null || bean.page.pageItems == null || bean.page.pageItems.size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }
        if (bean.page == null || bean.page.pageItems == null) {
            return;
        }

        mBeanList.addAll(bean.page.pageItems);
        mAdapter.notifyDataSetChanged();
        if (bean.page.pageItems.size() == 0 || bean.page.pageSize * bean.page.pageNumber >= bean.page.pageTotle) {
            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }

    @Override
    public void newBonusDonateSuccess(NewBonusDonateBean bean) {

    }

    @Override
    public void newGQSuccess(NewGQBean bean) {

    }
}
